package studio.raptor.extractor.segment.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import studio.raptor.extractor.common.constant.EventMetaStatus;
import studio.raptor.extractor.segment.config.ServerConfig;
import studio.raptor.extractor.segment.dao.EventDao;
import studio.raptor.extractor.segment.entity.EventMeta;
import studio.raptor.extractor.segment.service.EventMetaService;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

/**
 * Created by liuyong8 on 2017/7/24.
 */
@Component
@Scope("prototype")
public class EventActiveTask implements Runnable {

  private static final Logger log = LoggerFactory.getLogger(EventActiveTask.class);

  private ServerConfig serverConfig;

    private BlockingQueue<EventMeta> eventQueue;

  @Autowired
  private EventMetaService eventMetaService;

    @Override
  public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                // step1:查询eventMeta 为W状态的
                List<EventMeta> eventMetas = eventMetaService.findEventMetas(serverConfig.getKindId());

                // step2:将eventMeta放入队列
                for (EventMeta eventMeta : eventMetas) {
                    eventQueue.put(eventMeta);

                    // step3:修改表中eventMeta状态为P
                    eventMetaService.modifyEventMetaStatus2P(eventMeta);
                }
            } catch (Exception e) {
                log.error("active evets fail ", e);
            }
        }

    }

  public ServerConfig getServerConfig() {
    return serverConfig;
  }

  public void setServerConfig(ServerConfig serverConfig) {
    this.serverConfig = serverConfig;
  }

  public BlockingQueue<EventMeta> getEventQueue() {
    return eventQueue;
  }

  public void setEventQueue(BlockingQueue<EventMeta> eventQueue) {
    this.eventQueue = eventQueue;
  }
}
